define(function(require){
    var main =require('/static/main.js');
    require('/static/ajaxfileupload.js');
    var m = new main();
    function Cont(){
        this.Fun._init();
    }
    Cont.prototype.Fun = {
        TagIds:[],
        _init:function(){
            this.getlist();
            this.bindEvent();
        },
        bindEvent:function(){

        },
        getlist:function(){
            var param = {};
            var table1 = $('#datatable1').dataTable({
                'ordering':false,
                "serverSide": true,
                "searching":false,
                'bLengthChange': false,
                'bPaginate': true,
                "bDestory": true,
                "bRetrieve": true,
                "bFilter":false,
                "bSort": false,
                "bProcessing": true,
                "autoWidth": false,
                ajax: function (data, callback, settings) {
                    //封装请求参数
                    param.PageSize = data.length;//页面显示记录条数，在页面显示每页显示多少项的时候
                    param.start = data.start;//开始的记录序号
                    param.PageNo = (data.start / data.length)+1;//当前页码
                    //ajax请求数据
                    $.ajax({
                        type: "POST",
                        url: "/task/get-task-list",
                        cache: false,  //禁用缓存
                        data: param,  //传入组装的参数
                        dataType: "json",
                        success: function (result) {
                            var returnData = {};
                            returnData.draw = data.draw;
                            returnData.recordsTotal = result.total;
                            returnData.recordsFiltered = result.total;
                            returnData.data = result.data;
                            callback(returnData);
                        }
                    });
                },
                "aoColumns":[
                    {
                        "mDataProp": 'TaskLevel',
                        render:function(data){
                            var TaskLevel = '';
                            switch (data){
                                case 100:TaskLevel = '首次';break;
                                case 200:TaskLevel = '日常任务';break;
                                case 300:TaskLevel = '周任务';break;
                                case 400:TaskLevel = '月任务';break;
                                case 500:TaskLevel = '里程碑';break;
                            }
                            return TaskLevel;
                        }
                    },
                    {"mDataProp": 'TaskType',
                        render:function(data){
                            var TaskType = '';
                            switch (data){
                                case 100:TaskType = '完善资料';break;
                                case 200:TaskType = '累计销售订单';break;
                                case 210:TaskType = '累计预约订单';break;
                                case 220:TaskType = '累计活动订单';break;
                                case 300:TaskType = '累计引进粉丝';break;
                                case 310:TaskType = '累计引进用户';break;
                                case 320:TaskType = '累计引进会员';break;
                                case 330:TaskType = '累计客户总数';break;
                                case 400:TaskType = '累计推广次数';break;
                                case 500:TaskType = '累计绩效分数';break;
                                case 600:TaskType = '单篇文章阅读量';break;
                                case 700:TaskType = '单片文章成交量';break;
                            }
                            return TaskType;
                        }
                    },
                    {
                        "mDataProp": 'MytipLevelList',
                        render:function(data){
                            var TaskLevel = '';
                            var Connector = '';
                            var last = '';
                            if(data.length > 3){
                                last ='...';
                            }
                            $.each(data,function(k,v){
                                TaskLevel += Connector + v.LevelName;
                                Connector =',';
                                if(k == 2){
                                    TaskLevel += last;
                                    return false;
                                }
                            });
                            return TaskLevel;
                        }
                    },
                    { "mDataProp": "Summary"},
                    { "mDataProp": "TargetCnt"},
                    { "mDataProp": "BonusScore"},
                    { "mDataProp": "CreateTime",
                        render:function(data){
                            return m.formatTimeAll2(data);
                        }
                    },
                    {
                        "mDataProp": "TaskId",
                        "sClass": "option_btn",
                        "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                            $(nTd).html('<span class="EditBtn" data-id="'+sData+'">编辑</span>\
                            <span class="DelBtn" data-id="'+sData+'">删除</span>');

                        }
                    }
                ],
            }).api();

            // 点击搜索
            $('#btnSearch').click(function(){
                param.TaskLevel = $('.TaskLevel').val();
                var table = $('#datatable1').DataTable();
                table.ajax.reload();

            })
            // 点击重置按钮
            $('#btnRefresh').click(function(){
                $('.TaskLevel option').each(function(){
                    if(parseInt($(this).val()) == ''){
                        $(this).attr('selected',true);
                    }else{
                        $(this).attr('selected',false);
                    }
                })
                var table = $('#datatable1').DataTable();
                table.ajax.reload();
            })
            // 编辑
            $(document).on('click','.EditBtn',function(){
                var id = $(this).attr('data-id');
                layer.confirm('编辑任务会导致正在做该任务的用户信息重置哟，确认编辑吗？', {
                    btn: ['确认','取消'] //按钮
                }, function(){
                    window.location.href = '/task/createtask?taskid='+id;
                });

            })
            // 删除
            var isactive = false;
            $(document).on('click','.DelBtn',function(){
                if(isactive == true){
                    return false;
                }
                var id = $(this).attr('data-id');
                layer.confirm('删除任务会导致正在做该任务的用户信息重置哟，确认删除吗？', {
                    btn: ['确认','取消'] //按钮
                }, function(){
                    isactive = true;
                    $.ajax({
                        type: "POST",
                        url: "/task/task-del",
                        data :{TaskId:id},
                        dataType: "json",
                        success: function (ret){
                            isactive = false;
                            if(ret.IsBizSuccess){
                                layer.msg("删除成功");
                                var table1 = $('#datatable1').DataTable();
                                table1.ajax.reload();
                            }else{
                                layer.msg(ret.ErrMsg);
                            }
                        }
                    });

                });
            })
        }
    }
    var a = new Cont();

})